﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;

namespace YY.WCFLib
{
    public static class Common
    {

        /// <summary>
        /// 类型对象成员克隆
        /// </summary>
        /// <param name="os">克隆源</param>
        /// <param name="od">目标对象</param>        
        public static bool objEval(object os, object od)
        {
            try
            {
                PropertyInfo[] properties = os.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    object value = item.GetValue(os, null);
                    Type t = os.GetType();
                    t.GetProperty(name).SetValue(od, value, null);
                }
                return true;
            }
            catch { }
            return false;

        }

        /// <summary>
        /// 类型对象成员克隆
        /// </summary>
        /// <param name="os">克隆源</param>
        /// <param name="od">目标对象</param>
        /// <param name="exceptNames">排除的属性成员名称</param>
        /// <returns></returns> 
        public static bool objEval(object os, object od, List<string> exceptNames)
        {
            try
            {
                PropertyInfo[] properties = os.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    if (!exceptNames.Contains(name))
                    {
                        object value = item.GetValue(os, null);
                        Type t = os.GetType();
                        t.GetProperty(name).SetValue(od, value, null);
                    }
                }
                return true;
            }
            catch { }
            return false;

        }

        /// <summary>
        /// 返回上传文件服务器目录
        /// </summary>
        /// <param name="date">文件上传日期</param>
        /// <returns>文件所在目录</returns>
        public static string GetUploadFileDir(DateTime date)
        {
            return System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["AttachDir"], date.ToString("yyyyMMdd"));//文件目录
        }

        ///   <summary> 
        ///   将DataReader转换为DataTable 
        ///   </summary> 
        ///   <param   name= "reader "> 要被转换的DataReader </param> 
        ///   <returns> 转换后的DataTable </returns> 
        public static DataTable ConvertDataReaderToDataTable(IDataReader reader)
        {
            try
            {
                DataTable objDataTable = new DataTable();
                int intFieldCount = reader.FieldCount;

                //将列名添加到DataTable中 
                for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
                {
                    objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
                }

                //装入内容 
                objDataTable.BeginLoadData();

                object[] objValues = new object[intFieldCount];
                while (reader.Read())
                {
                    reader.GetValues(objValues);
                    objDataTable.LoadDataRow(objValues, true);
                }
                reader.Close();
                objDataTable.EndLoadData();

                return objDataTable;
            }
            catch { }
            return null;
        }

        public static IEnumerable<T> ConvertDataReaderToIEnumerable<T>(SqlDataReader dr) where T : new()
        {
            try
            {
                List<T> ary = new List<T>();
                while (dr.Read())
                {
                    T obj = new T();
                    PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                    foreach (PropertyInfo item in properties)
                    {
                        object value = null;
                        if (dr[item.Name] != DBNull.Value)
                            value = dr[item.Name];
                        item.SetValue(obj, value, null);
                    }
                    ary.Add(obj);
                }
                return ary;
            }
            catch { }
            return null;
        }

        public static DataTable GetSqlDataTable(string connectionString, string sqlCmd)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand(sqlCmd, con);
                    cmd.CommandType = CommandType.Text;
                    con.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    con.Close();
                    return ds.Tables[0];
                }
            }
            catch { }
            return null;
        }
    }
}
